package com.jy.modules.cims.component.sysAssist;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.jy.modules.cims.common.sysUtils.DateTimeUtils;
import com.jy.modules.cims.data.common.dto.TCMmRefundPlanDTO;

public class SettleDateImpl {

	/**
	 * @description 根据账单日生成 下一结算日期
	 * @param billDate
	 *            账单日
	 * @return Date 下一结算日期
	 * @author shengzhoutao
	 * @date 2015-07-21
	 */
	public static Date getNextSettleDateForBillDate(Long billDate) {
		Date settleDate = null;
		int currentDay = Calendar.getInstance().get(Calendar.DATE);
		Date currentDate = new Date();
		String dateStr = "";

		if (currentDay < billDate.intValue()) {
			dateStr = DateTimeUtils.formatDate(currentDate, DateTimeUtils.DEFAULT_DATE_FORMAT);
			dateStr = dateStr.substring(0, 6) + billDate;
		} else {
			currentDate = DateTimeUtils.addMonths(currentDate, 1);
			dateStr = DateTimeUtils.formatDate(currentDate, DateTimeUtils.DEFAULT_DATE_FORMAT);
			dateStr = dateStr.substring(0, 6) + billDate;
		}

		settleDate = DateTimeUtils.parse(dateStr, DateTimeUtils.DEFAULT_DATE_FORMAT);
		return settleDate;
	}
	
	/**
	 * 计算上一个交割日
	 * @Description 
	 * @param billDate
	 * @return
	 * @throws Exception   
	 * Date  
	 * @throws
	 * @author zhangmi
	 * @date 2016-5-30
	 */
	public static Date getBackSettleDateForBillDate(Long billDate) throws Exception{
		Date deliverDate = SettleDateImpl.getNextSettleDateForBillDate(billDate);
		deliverDate = DateTimeUtils.addMonths(deliverDate, -1);
		deliverDate = DateTimeUtils.parseDate(DateTimeUtils.formatDate(deliverDate, "yyyy-MM-dd"), "yyyy-MM-dd");
		return deliverDate;
	}
	
	/**
	 * @description 根据账单日生成 下一结算日期
	 *  @param currentDate
	 *            当前日期
	 * @param billDate
	 *            账单日
	 * @return Date 下一结算日期
	 * @author shengzhoutao
	 * @date 2015-07-21
	 */
	public static Date getNextSettleDateForBillDate(Date currentDate, Long billDate) {
		Date settleDate = null;
		
		Calendar calendar = Calendar.getInstance();
		calendar.setTime(currentDate);
		
		int currentDay = calendar.get(Calendar.DATE);
		String dateStr = "";

		if (currentDay < billDate.intValue()) {
			dateStr = DateTimeUtils.formatDate(currentDate, DateTimeUtils.DEFAULT_DATE_FORMAT);
			dateStr = dateStr.substring(0, 6) + billDate;
		} else {
			currentDate = DateTimeUtils.addMonths(currentDate, 1);
			dateStr = DateTimeUtils.formatDate(currentDate, DateTimeUtils.DEFAULT_DATE_FORMAT);
			dateStr = dateStr.substring(0, 6) + billDate;
		}

		settleDate = DateTimeUtils.parse(dateStr, DateTimeUtils.DEFAULT_DATE_FORMAT);
		return settleDate;
	}
	
	/**
	 * 
	 * @Description 根据结算计划取最小开始还款日期
	 * @param tcmmCostPlanDTOList
	 * @return   
	 * Date  
	 * @throws
	 * @author zhangmi
	 * @date 2016-3-1
	 */
	public static Date getMinStartDate(List<TCMmRefundPlanDTO> tcmmRefundPlanDTOList){
		int cnt = 0;
		for(int i = 1;i<tcmmRefundPlanDTOList.size();i++){
			cnt = tcmmRefundPlanDTOList.get(cnt).getCurrentTime()>tcmmRefundPlanDTOList.get(i).getCurrentTime()?i:cnt;
		}
		return tcmmRefundPlanDTOList.get(cnt).getStartDate();
	}
}
